package cate.game.role.res.treasureturntable;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDTreasureTurntable;
import cate.common.table.res.treasureturntable.row.TreasureTurntableBaseRow;
import cate.common.util.GameResult;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import cate.game.role.res.treasureturntable.msg.TreasureTurntableDataResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass("寻宝转盘")
public class MyTreasureTurntables extends FuncOpenChecker {
    @NoteField("转盘们")
    public Map<Byte, SingleTreasureTurntable> turntables;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (turntables == null) {
            turntables = new HashMap<>();
        }
        turntables.values().forEach(e -> e.initialize(role));
    }

    @Override
    public void onEnter() {
        turntables.values().forEach(SingleTreasureTurntable::onEnter);
    }

    public void checkInit() {
        List<Byte> types = role.getGame().table.treasureTurntable.base.getTypes();
        if (!types.isEmpty()) {
            boolean notice = false;
            for (Byte type : types) {
                if (!turntables.containsKey(type) && getTurntable(type) != null) {
                    notice = true;
                }
            }
            if (notice) {
                noticeUpdate();
            }
        }
    }

    @JsonIgnore
    public SingleTreasureTurntable getTurntable(byte type) {
        TreasureTurntableBaseRow row = role.getGame().table.treasureTurntable.base.getRow(type, role.getBase().level);
        if (row == null) {
            return null;
        }
        SingleTreasureTurntable t = turntables.computeIfAbsent(type, k -> {
            SingleTreasureTurntable treasureTurntable = new SingleTreasureTurntable(k, row.id);
            treasureTurntable.initialize(role);
            treasureTurntable.refreshItems();
            return treasureTurntable;
        });
        return t;
    }

    @Override
    public int getFuncId() {
        return GDFunc.TREASURE_TURNTABLE;
    }

    @Override
    public void onAfterOpen() {

    }

    @Override
    public void onDaySpan(boolean silence) {
        turntables.values().forEach(e -> e.onDaySpanSafe(silence));
        noticeUpdate();
    }

    public void noticeUpdate() {
        role.sendNow(new TreasureTurntableDataResp(this));
    }

    public GameResult<Void> drawItems(byte turnTableType, byte drawType) {
        GameResult<Void> r = new GameResult<>();
        SingleTreasureTurntable turntable = getTurntable(turnTableType);
        if (turntable == null) {
            return r.fail("错误的转盘类型");
        }
        if (drawType != GDTreasureTurntable.SINGLE_DRAW && drawType != GDTreasureTurntable.MULTI_DRAW) {
            return r.fail("错误的抽奖类型");
        }
        return turntable.drawItems(drawType);
    }

    public GameResult<Void> refresh(byte turntableType, boolean refresh) {
        GameResult<Void> r = new GameResult<>();
        SingleTreasureTurntable turntable = getTurntable(turntableType);
        if (turntable == null) {
            return r.fail("错误的转盘类型");
        }
        return turntable.refresh(refresh);
    }

    public GameResult<Void> getScoreReward(byte turntableType, int rewardId) {
        GameResult<Void> r = new GameResult<>();
        SingleTreasureTurntable turntable = getTurntable(turntableType);
        if (turntable == null) {
            return r.fail("错误的转盘类型");
        }
        return turntable.getScoreReward(rewardId);
    }
}
